package com.cxg.fairy.ec.detail;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.ListViewCompat;
import android.view.View;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.cxg.fairy.app.AccountManager;
import com.cxg.fairy.delegates.LatteDelegate;
import com.cxg.fairy.net.RestClient;
import com.cxg.fairy.net.callback.IError;
import com.cxg.fairy.net.callback.ISuccess;
import com.cxg.fairy.ui.recycler.MultipleItemEntity;
import com.cxg.fairy.util.log.LatteLogger;
import com.diabin.latte.ec.R;
import com.diabin.latte.ec.R2;
import com.hedgehog.ratingbar.RatingBar;
import com.rengwuxian.materialedittext.MaterialEditText;

import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by gabrielsung on 2018/4/6 0006.
 */

public class CommentActionDelegate extends LatteDelegate implements ISuccess{

    private float tradeRatingCount = 0;

    @OnClick(R2.id.back_home)
    void onClickBackHome() {
        getSupportDelegate().pop();
    }

    @BindView(R2.id.lv_comment)
    ListViewCompat commentListView = null;

    @BindView(R2.id.rst_comment_trade_start)
    RatingBar commentTradeStar = null;

    @BindView(R2.id.met_comment_trade)
    MaterialEditText commentTradeContent = null;

    @OnClick(R2.id.btn_comment_action_commit)
    void onClickCommit() {
        String tradeContent = commentTradeContent.getText().toString();
        CommentActionAdapter mAdapter = (CommentActionAdapter) commentListView.getAdapter();
        JSONArray arr = mAdapter.getOrderCommentArray();
        RestClient.builder()
                .url("order/orderGoodsEvaluate")
                .params("uuid", AccountManager.getSignUUID())
                .params("orderNo", getArguments().getString(COMMENT_ORDER_NO))
                .params("list_orderGoodsEval", JSONArray.toJSONString(arr))
                .params("speedStar", tradeRatingCount)
                .params("distriEval",tradeContent)
                .loader(getContext())
                .success(new ISuccess() {
                    @Override
                    public void onSuccess(String response) {
                        LatteLogger.e("response=",response);
                    }
                })
                .error(new IError() {
                    @Override
                    public void onError(int code, String msg) {
                        LatteLogger.e("code=",code+"|msg="+msg);
                    }
                })
                .build()
                .post();
        LatteLogger.e("list_orderGoodsEval=",JSONArray.toJSONString(arr));
        LatteLogger.e("tradeRatingCount=",tradeRatingCount+"");
        LatteLogger.e("tradeContent=",tradeContent);
    }

    private static final String COMMENT_ORDER_NO = "COMMENT_ORDER_NO";

    @Override
    public Object setLayout() {
        return R.layout.delegate_goods_comment_action;
    }

    @Override
    public void onBindView(@Nullable Bundle savedInstanceState, @NonNull View rootView) {
        initData(getArguments().getString(COMMENT_ORDER_NO));

        commentTradeStar.setOnRatingChangeListener(
                new RatingBar.OnRatingChangeListener() {
                    @Override
                    public void onRatingChange(float RatingCount) {
                        tradeRatingCount = RatingCount;
                    }
                }
        );
    }

    public static CommentActionDelegate create(String orderNo) {
        final Bundle args = new Bundle();
        args.putString(COMMENT_ORDER_NO, orderNo);
        final CommentActionDelegate delegate = new CommentActionDelegate();
        delegate.setArguments(args);
        return delegate;
    }

    private void initData(String orderNo) {
        RestClient.builder()
                .loader(getContext())
                .url("order/getOrderGoods")
                .params("uuid", AccountManager.getSignUUID())
                .params("orderNo",orderNo)
                .success(this)
                .build()
                .post();
    }

    @Override
    public void onSuccess(String response) {
        LatteLogger.e("response=", response);
        List<MultipleItemEntity> data = new CommentActionDataConverter().setJsonData(response).convert();
        final CommentActionAdapter commentListAdapter = new CommentActionAdapter(getContext(), R.layout.item_comments_action_list, data,this);
        commentListView.setDividerHeight(0);
        commentListView.setAdapter(commentListAdapter);
    }
}
